package me.numark.fragments;

import com.astuetz.PagerSlidingTabStrip;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import me.numark.R;

/**
 * Created by tian on 14-9-18:上午7:43.
 */
public class TimelineFragment extends Fragment implements View.OnClickListener {

    private ViewPager pager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_timeline, container, false);
        pager = (ViewPager) v.findViewById(R.id.pager);
        pager.setAdapter(new TimelineFragmentPageAdapter());
        PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) v.findViewById(R.id.tabs);
        tabs.setViewPager(pager);
        return v;
    }

    @Override
    public void onClick(View v) {

    }

    private NewsFeedFragment mFeedFragment;

    private NewsFeedFragment mHotFragment;

    private class TimelineFragmentPageAdapter extends FragmentPagerAdapter {

        public TimelineFragmentPageAdapter() {
            super(getChildFragmentManager());
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    return getFeedFragment();
                case 1:
                    return getHotFragment();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return getString(R.string.mine);
                case 1:
                    return getString(R.string.hot_timeline);
                default:
                    return "";
            }
        }
    }

    private Fragment getFeedFragment() {
        Bundle args = new Bundle();
        args.putInt(NewsFeedFragment.TYPE, NewsFeedFragment.TYPE_TIMELINE);
        if (mFeedFragment == null) {
            mFeedFragment = (NewsFeedFragment) NewsFeedFragment
                    .instantiate(getActivity(), NewsFeedFragment.class.getName(), args);
        }
        return null;
    }

    private Fragment getHotFragment() {
        Bundle args = new Bundle();
        args.putInt(NewsFeedFragment.TYPE, NewsFeedFragment.TYPE_HOT);
        if (mHotFragment == null) {
            mHotFragment = (NewsFeedFragment) NewsFeedFragment
                    .instantiate(getActivity(), NewsFeedFragment.class.getName(), args);
        }
        return null;
    }

}
